﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Models.Wishire.entity;
using Models.Wishire.Operator;

namespace Models.Wishire
{
    internal class solve
    {
        //根据accountId和cityname查询planId
        int w = 1;
        public int find_planId(int accountId,string name)
        {
            opdataplan op = new opdataplan();
            var ans = op.planFind_Id(accountId, name);
            return ans.GetPlanId();
        }
        public List<plan_st> find_plan(int accountId, string city)
        {
            opdataplan op = new opdataplan();
            var ans = op.planFind_Id_City(accountId, city);
            return ans;
        }
        public List<answer> find_answerby_planId(int planId)
        {
            opdataanswer op = new opdataanswer();
            return op.answerFind_planId(planId);
        }
        //给定planId查询生成方案
        public List<answer> generate(int planId)
        {
            opdataanswer opans = new opdataanswer();
            if(opans.answerFind_planId(planId).Count != 0)
            {
                opans.answerDeleteall(planId);
            } 
            opdataplan op = new opdataplan();
            var plan = op.find_planby_planId(planId);
            Travel travel = new Travel();
            List<target_st> target = new List<target_st>();
            opdatatarget optarget = new opdatatarget();
            target = optarget.targetFind_planId(planId);
            travel.Set_target(target);
            travel.planId = planId;
            //设置time
            string time1 = plan.GetStartTime();
            string time2 = plan.GetEndTime();
            DateTime date1 = DateTime.Parse(time1.Substring(0, time1.Length - 2));
            DateTime date2 = DateTime.Parse(time2.Substring(0, time2.Length - 2));
            TimeSpan difference = date2 - date1;
            travel.times = difference.Days - 1;
            travel.day1 = 3 - time1[11] + '0';
            travel.day2 = time2[11] - '0'+1;
            //设置出发点
            target_st start_place = new target_st();
            start_place.SetTargetLat(plan.GethotelLat());
            start_place.SetTargetLng(plan.GethotelLng());
            start_place.SetTargetCity(plan.GetCityName());
            start_place.SetTargetAddress(plan.GethotelName());
            travel.Set_startPlace(start_place);
            //设置出发点
            //temp.Add_target()
            city city = new city();
            opdatacity tmp = new opdatacity();
            var ori = tmp.cityFind_all();
            for (int i = 0; i < ori.Count; i++)
                if (ori[i].GetCityName() == start_place.GetTargetCity() || ori[i].GetCityName() + "市" == start_place.GetTargetCity())
                {
                    target_st ori_city = new target_st();
                    ori_city.SetTargetLat(ori[i].GetLat());
                    ori_city.SetTargetLng(ori[i].GetLng());
                    ori_city.SetTargetCity(ori[i].GetCityName());
                    ori_city.SetTargetName(ori[i].GetName());
                    ori_city.SetTargetType(ori[i].GetType());
                    travel.Add_target(ori_city);

                }
            travel.init();
            /*--------------计算初始化*/
            Console.WriteLine(travel.dist[0, 1]);
            List<answer> ans = travel.generate();
            return ans;
        }
        //给定planId和两个次序，交换两个生成方案
        public void generate_swap(int planId,int index1,int index2)
        {
            opdataanswer opans = new opdataanswer();
            opans.swapanswer(planId,index1,index2);
        }
        //给定planId和index，删除
        public void generate_delete(int planId, int index)
        {
            opdataanswer opans = new opdataanswer();
            opans.answerDelete(planId, index);
        }
        //插入plan,新建一个plan
        public void plan_insert(plan_st plan)
        {
            opdataplan opdataplan = new opdataplan();
            opdataplan.planInsert(plan);
        }
        //插入target,新建一个target
        public void target_insert(target_st target)
        {
            opdatatarget op = new opdatatarget();
            op.targetInsert(target);
        }
        //根据planId删除一个
        public void delete_byplanId(int planId)
        {
            opdataplan op = new opdataplan();
            op.planDelete(planId);
        }
        //取得所有有plan的城市
        public List<plan_st> get_allcity(int accountId) 
        {
            opdataplan op = new opdataplan();
            var plan = op.planFind_accoundId(accountId);
            List<plan_st> hasans = new List<plan_st>();
            for(int i = 0; i < plan.Count; i++)
            {
                opdataanswer opp = new opdataanswer();
                if (opp.answerFind_planId(plan[i].GetPlanId()).Count != 0)
                    hasans.Add(plan[i]);
            }
            return hasans;
            List<string> ans = new List<string>();
            for(int i=0;i<plan.Count();i++)
            {
                string name = plan[i].GetCityName();
                if (!ans.Contains(name))
                {
                    // 如果不存在，则添加到列表中
                    ans.Add(name);
                }
            }
           // return ans;
        }
        public void update_plan(plan_st plan)
        {
            int id = plan.GetPlanId();
            opdataplan op = new opdataplan();
            op.planUpdate(plan);
        }
        public List<target_st> find_all_target (int planId)
        {
            opdatatarget op = new opdatatarget();
            var ans = op.targetFind_planId(planId);
            return ans;
        }
        public void update_target(target_st target)
        {
            int id = target.GetPlanId();
            opdatatarget op = new opdatatarget();
            op.targetUpdate(target, id);
        }
        public void delete_target(int targetId)
        {
            opdatatarget op = new opdatatarget();
            op.targetDelete(targetId);
        }
        public List<answer> find_planin_answer()
        {
            opdataanswer op = new opdataanswer();
            return op.answerFindall();
        }
        public plan_st findplan_byId(int id)
        {
            opdataplan op = new opdataplan();
            return op.find_planby_planId(id);
        }
        /*public void delete_answer()
        {
            
        }*/
        public List<int> find_all_generated_planId(int accountId)//all planId
        {
            opdataanswer op = new opdataanswer();
            List<plan_st> ans = op.answerFind_accountId(accountId);
            List<int> finaans = new List<int>();
            for(int i = 0; i < ans.Count; i++)
            {
                if(!finaans.Contains(ans[i].GetPlanId())) finaans.Add(ans[i].GetPlanId());
            }
            return finaans;
        }
        public plan_st find_planby_planId(int planId)//all planId
        {
            opdataplan op = new opdataplan();
            plan_st ans = op.find_planby_planId(planId);
            return ans;
        }
    }
}
